<?php
/**
 * SetpointController
 *
 */
class SetpointController extends Zend_Controller_Action 
{
    protected $model;
    protected $mote_gateway;
 
    protected function _getModel()
    {
        if (null === $this->model) {
            require_once APPLICATION_PATH . '/models/Setpoint.php';
            $this->model = new Model_Setpoint();
        }
        return $this->model;
    }    
     
    protected function _getMoteGatewayModel()
    {
        if (null === $this->mote_gateway) {
            require_once APPLICATION_PATH . '/models/MoteGateway.php';
            $this->mote_gateway = new Model_MoteGateway();
        }
        return $this->mote_gateway;
    }
    
    protected function _getSettingModel() 
    {
      if (null === $this->setting_model) {
          require_once APPLICATION_PATH . '/models/Setting.php';
          $this->setting_model = new Model_Setting();
      }
      return $this->setting_model;
    }
    
    protected function _getSetpointForm()
    {
        require_once APPLICATION_PATH . '/forms/Setpoint.php';
        $form = new Form_Setpoint();
        $form->setAction('ac2/setpoint/edit');
        return $form;
    }
    
    protected function scaleSetpoint($setpoint, $scale) {
      if ($scale == "C") { // only save F in DB
        require_once APPLICATION_PATH . '/models/Reading.php';
        return Model_Reading::toFahrenheit($setpoint);
      }
      return $setpoint;
    }
    

    public function indexAction() 
    {
      return $this->_helper->redirector->gotoSimple('index','index');
    }


    
    public function editAction() 
    {
      $request = $this->getRequest();
      $scale  = ($this->_getSettingModel()->isCelsius() == true) ? "C":"F";
      $form    = $this->_getSetpointForm();
      $form->getForm($scale);

      // check to see if this action has been POST'ed to
      if ($this->getRequest()->isPost()) {
          
          // now check to see if the form submitted exists, and
          // if the values passed in are valid for this form
          if ($form->isValid($request->getPost())) {
              
              // since we now know the form validated, we can now
              // start integrating that data sumitted via the form
              // into our model
              $model        = $this->_getModel();
              $mote_gateway = $this->_getMoteGatewayModel();
              $values       = $form->getValues();
              $values['setpoint'] = $this->scaleSetpoint($values['setpoint'], $scale);              
              
              $model->update($values);
              $mote_gateway->updateSetpoint($values['room_id'], $values['setpoint']); 
              return $this->_helper->redirector->gotoSimple('index','index');
          }
      }

      $room_id = $request->getParam("room_id");
      $spModel = $this->_getModel();
      $sp      = $spModel->fetchEntry($room_id);

      if ($scale == "C") { // only save F in DB
        require_once APPLICATION_PATH . '/models/Reading.php';
        $sp['setpoint']  = round(Model_Reading::toCelsius($sp['setpoint']), Model_Reading::getPrecision());
      }
      
      $data = array('setpoint'=>$sp['setpoint'],
                    'room_id' =>$sp['room_id'],
                    'zone_id' =>$sp['zone_id']
                   );
      $form->populate($data);
      
      $this->view->sp = $sp;
      $this->view->form = $form;
      $this->view->scale  = $scale;
      $this->view->nav  = "";
    }

}
